<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 8.10: Approximate linear discrimination via linear programming</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/svm_1.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 8.10: Approximate linear discrimination via linear programming</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.6.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Joelle Skaf - 10/16/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The goal is to find a function f(x) = a'*x - b that classifies the non-</span>
<span class="comment">% separable points {x_1,...,x_N} and {y_1,...,y_M} by allowing some</span>
<span class="comment">% misclassification. a and b can be obtained by solving the following</span>
<span class="comment">% problem:</span>
<span class="comment">%           minimize    1'*u + 1'*v</span>
<span class="comment">%               s.t.    a'*x_i - b &gt;= 1 - u_i        for i = 1,...,N</span>
<span class="comment">%                       a'*y_i - b &lt;= -(1 - v_i)     for i = 1,...,M</span>
<span class="comment">%                       u &gt;= 0 and v &gt;= 0</span>

<span class="comment">% data generation</span>
n = 2;
randn(<span class="string">'state'</span>,2);
N = 50; M = 50;
Y = [1.5+0.9*randn(1,0.6*N), 1.5+0.7*randn(1,0.4*N);
     2*(randn(1,0.6*N)+1), 2*(randn(1,0.4*N)-1)];
X = [-1.5+0.9*randn(1,0.6*M),  -1.5+0.7*randn(1,0.4*M);
      2*(randn(1,0.6*M)-1), 2*(randn(1,0.4*M)+1)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span> <span class="string">u(N)</span> <span class="string">v(M)</span>
    minimize (ones(1,N)*u + ones(1,M)*v)
    X'*a - b &gt;= 1 - u;
    Y'*a - b &lt;= -(1 - v);
    u &gt;= 0;
    v &gt;= 0;
cvx_end

<span class="comment">% Displaying results</span>
linewidth = 0.5;  <span class="comment">% for the squares and circles</span>
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
tt = linspace(t_min-1,t_max+1,100);
p = -a(1)*tt/a(2) + b/a(2);
p1 = -a(1)*tt/a(2) + (b+1)/a(2);
p2 = -a(1)*tt/a(2) + (b-1)/a(2);

graph = plot(X(1,:),X(2,:), <span class="string">'o'</span>, Y(1,:), Y(2,:), <span class="string">'o'</span>);
set(graph(1),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'MarkerFaceColor'</span>,[0 0.5 0]);
hold <span class="string">on</span>;
plot(tt,p, <span class="string">'-r'</span>, tt,p1, <span class="string">'--r'</span>, tt,p2, <span class="string">'--r'</span>);
axis <span class="string">equal</span>
title(<span class="string">'Approximate linear discrimination via linear programming'</span>);
<span class="comment">% print -deps svc-discr.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling SDPT3: 203 variables, 100 equality constraints
------------------------------------------------------------

 num. of constraints = 100
 dim. of linear var  = 200
 dim. of free   var  =  3 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|9.1e-01|1.9e+01|4.1e+04| 1.414214e+03  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.968|1.2e-06|7.1e-01|2.6e+03| 1.241114e+03  1.166168e+01| 0:0:00| chol  1  1 
 2|1.000|0.567|2.7e-06|3.1e-01|1.0e+03| 4.476059e+02  8.828967e+00| 0:0:00| chol  1  1 
 3|0.910|0.846|1.6e-06|4.9e-02|1.4e+02| 6.788544e+01  6.105312e+00| 0:0:00| chol  1  1 
 4|0.940|0.640|6.2e-06|1.8e-02|6.3e+01| 3.280721e+01  4.752207e+00| 0:0:00| chol  1  1 
 5|0.982|0.439|6.7e-07|1.0e-02|2.8e+01| 1.368275e+01  4.223464e+00| 0:0:00| chol  1  1 
 6|1.000|0.754|7.9e-09|2.5e-03|1.1e+01| 1.107127e+01  4.125359e+00| 0:0:00| chol  1  1 
 7|1.000|0.241|5.0e-08|1.9e-03|8.2e+00| 9.815542e+00  4.091411e+00| 0:0:00| chol  1  1 
 8|1.000|0.587|1.5e-07|7.7e-04|5.0e+00| 8.915113e+00  4.980641e+00| 0:0:00| chol  1  1 
 9|0.921|0.455|8.2e-08|4.2e-04|2.4e+00| 7.197481e+00  5.289285e+00| 0:0:00| chol  1  1 
10|1.000|0.524|2.6e-08|2.0e-04|1.4e+00| 6.805736e+00  5.594202e+00| 0:0:00| chol  1  1 
11|1.000|0.299|1.3e-08|1.4e-04|1.0e+00| 6.562960e+00  5.719608e+00| 0:0:00| chol  1  1 
12|1.000|0.516|5.4e-09|6.8e-05|6.2e-01| 6.439207e+00  5.882265e+00| 0:0:00| chol  1  1 
13|0.755|0.303|2.5e-09|4.7e-05|4.0e-01| 6.292514e+00  5.919046e+00| 0:0:00| chol  1  1 
14|1.000|0.303|5.3e-10|3.3e-05|4.1e-01| 6.372321e+00  5.996720e+00| 0:0:00| chol  2  1 
15|0.983|0.306|1.1e-09|2.3e-05|2.4e-01| 6.245623e+00  6.016152e+00| 0:0:00| chol  1  1 
16|1.000|0.679|1.4e-10|7.3e-06|7.8e-02| 6.169151e+00  6.095341e+00| 0:0:00| chol  1  1 
17|0.993|0.899|7.8e-11|7.4e-07|6.7e-03| 6.149220e+00  6.142910e+00| 0:0:00| chol  1  1 
18|0.987|0.986|1.1e-10|3.1e-06|1.4e-04| 6.148578e+00  6.148492e+00| 0:0:00| chol  1  1 
19|0.996|0.989|3.9e-13|6.5e-08|3.5e-06| 6.148570e+00  6.148568e+00| 0:0:00| chol  1  1 
20|1.000|0.989|5.3e-14|1.6e-09|7.9e-08| 6.148569e+00  6.148569e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 20
 primal objective value =  6.14856945e+00
 dual   objective value =  6.14856940e+00
 gap := trace(XZ)       = 7.92e-08
 relative gap           = 5.96e-09
 actual relative gap    = 4.14e-09
 rel. primal infeas     = 5.30e-14
 rel. dual   infeas     = 1.60e-09
 norm(X), norm(y), norm(Z) = 8.9e+01, 2.4e+00, 1.0e+01
 norm(A), norm(b), norm(C) = 7.3e+01, 1.1e+01, 1.1e+01
 Total CPU time (secs)  = 0.22  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 2.9e-13  0.0e+00  8.8e-09  0.0e+00  4.1e-09  6.0e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +6.14857
 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="svm_1__01.png" alt=""> 
</div>
</div>
</body>
</html>